<?php
class ChitieungaysController extends AppController {

	var $name = 'Chitieungays';
	var $helpers=array('Js');
	var $uses = array('Chitieungay','Chitieunv','Kehoachbhld','Phankenh','Diadiem','Kenh','Nhansubhld','Hanghoabhld','Thuocdv');
	var $components = array('RequestHandler');
	
	function index() {
		$this->Chitieungay->recursive = 0;
		$this->set('chitieungays', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid chitieungay', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('chitieungay', $this->Chitieungay->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Chitieungay->create();
			if ($this->Chitieungay->save($this->data)) {
				$this->Session->setFlash(__('The chitieungay has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The chitieungay could not be saved. Please, try again.', true));
			}
		}
		$kehoachbhlds = $this->Chitieungay->Kehoachbhld->find('list');
		$hanghoas = $this->Chitieungay->Hanghoa->find('list');
		$this->set(compact('kehoachbhlds', 'hanghoas'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid chitieungay', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Chitieungay->save($this->data)) {
				$this->Session->setFlash(__('The chitieungay has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The chitieungay could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Chitieungay->read(null, $id);
		}
		$kehoachbhlds = $this->Chitieungay->Kehoachbhld->find('list');
		$hanghoas = $this->Chitieungay->Hanghoa->find('list');
		$this->set(compact('kehoachbhlds', 'hanghoas'));
	}

	function xoakehoach($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Kế hoạch không tồn tại', true));
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		if ($this->Kehoachbhld->delete($id)) {
			$this->Session->setFlash(__('Đã xóa kế hoạch', true));
			$this->redirect(array('controller'=>'chitieungays','action'=>'hienkehoach',$this->Session->read('cur_manv')));
		}
		$this->Session->setFlash(__('Không thế xóa. vui lòng thử lại', true));
		$this->redirect(array('controller'=>'chitieungays','action'=>'hienkehoach',$this->Session->read('cur_manv')));
	}

	function suachitieu($id=null)
	{
		if(!$id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		if(!empty($this->data))
		{	
			$record['Kehoachbhld']['id']=$this->data['Chitieungay']['idkh'];
			$record['Kehoachbhld']['tendoi']=$this->data['Chitieungay']['doi'];
			if(isset($this->data['Chitieungay']['cap2']))
				$cap2 = $this->data['Chitieungay']['cap2'];
			else
				$cap2 = $this->data['cap2'];
			
			if(isset($this->data['Chitieungay']['cap3']))
				$cap3 = $this->data['Chitieungay']['cap3'];
			else
				$cap3 = $this->data['cap3'];
				
			if(isset($this->data['Chitieungay']['cap4']))
				$cap4 = $this->data['Chitieungay']['cap4'];
			else
				$cap4 = $this->data['cap4'];
			
			$ddc1 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$this->data['Chitieungay']['cap1'])));
			$ddc2 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$cap2)));
			$ddc3 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$cap3)));
			$ddc4 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$cap4)));
			$diad =$ddc1['Diadiem']['tendd'].', '. $ddc2['Diadiem']['tendd'].', '.$ddc3['Diadiem']['tendd'].', '.$ddc4['Diadiem']['tendd'];
			$record['Kehoachbhld']['diadiem']=$diad;
			$record['Kehoachbhld']['diadiem_id']=$cap4;
			$record['Kehoachbhld']['thoigian']=date('Y-m-d',strtotime($this->data['Chitieungay']['thoigian']));
			$ns = $this->Kenh->find('list',array('conditions'=>array('id'=>$this->data['Chitieungay']['thanhvien']),'fields'=>array('id','tenkenh')));
			$nhans ='';
			foreach ($ns as $keyns => $valuens)
			{
				$nhans .=$valuens.', ';
			}
			$record['Kehoachbhld']['nhansu']=$nhans;
			$record['Kehoachbhld']['ghichu']=$this->data['Chitieungay']['ghichu'];
			$this->Kehoachbhld->create();
			if(!$this->Kehoachbhld->save($record))
			{
				$this->Session->setFlash(__('Không thể Lưu. vui lòng thử lại', true));
			}
			else
			{
				
				$kehoachbhld_id = $this->data['Chitieungay']['idkh'];
				$str = 'delete from nhansubhlds where kehoachbhld_id= '.$kehoachbhld_id;
				$this->Chitieungay->query($str);
				//debug($str);
				$nhansu = $this->data['Chitieungay']['thanhvien'];
				foreach ($nhansu as $value)
				{
					$str = 'insert into nhansubhlds(kehoachbhld_id,kenh_id) values('.$kehoachbhld_id.','.$value.')';
					$this->Chitieungay->query($str);
				}
				//$this->Chitieungay->create();
				$record = array();
				$chitieus =array_keys($this->data['Chitieungay']['hanghoa']);
				foreach ($chitieus as $ct)
				{
					$record['Chitieungay']['id']=$ct;
					$record['Chitieungay']['soluong']=($this->data['Chitieungay']['hanghoa'][$ct]==''?0:$this->data['Chitieungay']['hanghoa'][$ct]);
					$this->Chitieungay->saveAll($record);
				}
				$this->redirect(array('controller'=>'kehoachbhlds','action'=>'hienkehoach',$this->data['Chitieungay']['nhanvien_id']));
			}
			
		}
		$nhanvien_id = $this->Session->read('cur_manv');
		//$this->Kehoachbhld->unbindModel(array('hasMany'=>array('Chitieungay','Nhansubhld'),'belongsTo'=>array('Nhanvien')));
		$chitieungays = $this->Kehoachbhld->find('first',array(
											'recursive'=>1,
											'conditions'=>array('Kehoachbhld.id'=>$id)));
		
		$hanghoas = $this->Chitieungay->find('list',array('recursive'=>0,
											'conditions'=>array('Kehoachbhld.id'=>$id),
											'fields'=>array('Hanghoa.id','Hanghoa.tenhh')));
		
		
		$donvi_id = $this->Session->read('cur_dv');
		$ngay = $chitieungays['Kehoachbhld']['thoigian'];
		
		$this->Nhanvien->displayField='id';
		$nhanvien_id =$this->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id)));
		
		$nsbhld = $this->Kehoachbhld->Nhansubhld->find('list',array(
											'recursive'=>0,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'Kehoachbhld.thoigian'=>$ngay,
																'NOT'=>array('Kehoachbhld.id'=>$id)),
											'fields'=>array('kenh_id')));
		$kenhs = $this->Phankenh->find('list',array(
											'recursive'=>0,
											'fields'=>array('Kenh.id','Kenh.tenkenh'),
											'conditions'=>array('Nhanvien_id'=>$nhanvien_id,
																'ngaykt_hd'=>null,
																'Kenh.loaikenh_id'=>3,
																'NOT'=>array('kenh_id'=>$nsbhld))));
		/*$kenhs = $this->Phankenh->find('list',array(
											'recursive'=>0,
											'fields'=>array('Kenh.id','Kenh.tenkenh'),
											'conditions'=>array('Nhanvien_id'=>$nhanvien_id,
																'ngaykt_hd'=>null,
																'Kenh.loaikenh_id'=>3)));
																*/
		$diadiems = $this->Diadiem->find('list',array('conditions'=>array('bac'=>0),
														'fields'=>array('id','tendd')));
		
		$ddc4 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$chitieungays['Kehoachbhld']['diadiem_id'])));
		$ddc3 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$ddc4['Diadiem']['diadiem_id'])));
		$ddc2 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$ddc3['Diadiem']['diadiem_id'])));
		$ddc1 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$ddc2['Diadiem']['diadiem_id'])));

		$ddiemsc2 = $this->Diadiem->find('list',array('conditions'=>array('diadiem_id'=>$ddc1['Diadiem']['id']),
														'fields'=>array('id','tendd')));
		$ddiemsc3 = $this->Diadiem->find('list',array('conditions'=>array('diadiem_id'=>$ddc2['Diadiem']['id']),
														'fields'=>array('id','tendd')));
		$ddiemsc4 = $this->Diadiem->find('list',array('conditions'=>array('diadiem_id'=>$ddc3['Diadiem']['id']),
														'fields'=>array('id','tendd')));
		
		$nhansu = $this->Nhansubhld->find('list',array('conditions'=>array('kehoachbhld_id'=>$id),'fields'=>array('kenh_id')));

		$this->set(compact('chitieungays','hanghoas','id','kenhs','diadiems','nhansu','ddc4','ddc3','ddc2','ddc1','ddiemsc2','ddiemsc3','ddiemsc4'));
	}
	
	function themchitieu($ngay=null)
	{
		$nhanvien_id = $this->Session->read('cur_manv');
		if(!empty($this->data))
		{
			$record['Kehoachbhld']['tendoi']=$this->data['Chitieungay']['doi'];
			
			$ddc1 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$this->data['Chitieungay']['cap1'])));
			$ddc2 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$this->data['cap2'])));
			$ddc3 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$this->data['cap3'])));
			$ddc4 = $this->Diadiem->find('first',array('conditions'=>array('id'=>$this->data['cap4'])));
			$diad =$ddc1['Diadiem']['tendd'].', '. $ddc2['Diadiem']['tendd'].', '.$ddc3['Diadiem']['tendd'].', '.$ddc4['Diadiem']['tendd'];
			$record['Kehoachbhld']['diadiem']=$diad;
			$record['Kehoachbhld']['diadiem_id']=$this->data['cap4'];
			$record['Kehoachbhld']['thoigian']=date('Y-m-d',strtotime($this->data['Chitieungay']['thoigian']));
			$ngay = date('Y-m-d',strtotime($this->data['Chitieungay']['thoigian']));
			$ns = $this->Kenh->find('list',array('conditions'=>array('id'=>$this->data['Chitieungay']['thanhvien']),'fields'=>array('id','tenkenh')));
			$nhans ='';
			foreach ($ns as $keyns => $valuens)
			{
				$nhans .=$valuens.', ';
			}
			$record['Kehoachbhld']['nhansu']=$nhans;
			$record['Kehoachbhld']['ghichu']=$this->data['Chitieungay']['ghichu'];
			$record['Kehoachbhld']['nhanvien_id']=$nhanvien_id;
			$this->Kehoachbhld->create();
			
			if(!$this->Kehoachbhld->save($record))
			{
				$this->Session->setFlash(__('Không thể Lưu. vui lòng thử lại', true));
			}
			else
			{
				$kehoachbhld_id = $this->Kehoachbhld->id;
				//nhan su
				$nhansu = $this->data['Chitieungay']['thanhvien'];
				foreach ($nhansu as $value)
				{
					$str = 'insert into nhansubhlds(kehoachbhld_id,kenh_id) values('.$kehoachbhld_id.','.$value.')';
					$this->Chitieungay->query($str);
				}
				$record = array();
				$record['Chitieungay']['kehoachbhld_id']=$kehoachbhld_id;
				$hanghoas =array_keys($this->data['Chitieungay']['hanghoa']);
				foreach ($hanghoas as $hh)
				{
					$record['Chitieungay']['hanghoa_id']=$hh;
					$record['Chitieungay']['soluong']=($this->data['Chitieungay']['hanghoa'][$hh]==''?0:$this->data['Chitieungay']['hanghoa'][$hh]);
					$this->Chitieungay->saveAll($record);
				}
				$this->redirect(array('action'=>'themchitieu',$ngay));
			}
			
		}
		if(!$ngay)
		{
			$ngay = date('d-m-Y', time()+86400);			
			$con = array('DATEDIFF(Kehoachbhld.thoigian,NOW())'=>1);
		}
		else
		{
			$con = array('Kehoachbhld.thoigian'=>$ngay);
		}
	
		$chitieungays = $this->Kehoachbhld->find('all',array(
											'recursive'=>1,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																$con)));
		//// lay kenh cua tat ca nhan vien trong trung tam
		$donvi_id = $this->Session->read('cur_dv');
		$this->Nhanvien->displayField='id';
		$nhanvien_id =$this->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id)));
		$nsbhld = $this->Kehoachbhld->Nhansubhld->find('list',array(
											'recursive'=>0,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																$con),
											'fields'=>array('kenh_id')));
		$kenhs = $this->Phankenh->find('list',array(
											'recursive'=>0,
											'fields'=>array('Kenh.id','Kenh.tenkenh'),
											'conditions'=>array('Nhanvien_id'=>$nhanvien_id,
																'ngaykt_hd'=>null,
																'Kenh.loaikenh_id'=>3,
																'NOT'=>array('kenh_id'=>$nsbhld))));
		
		//$dv = $this->Session->read('cur_dv');
		$ngay = date('Y-m-d');
		$hanghoas = $this->Hanghoabhld->find('list',array('fields'=>array('Hanghoabhld.hanghoa_id','Hanghoabhld.tenhh'),'order'=>array('Hanghoabhld.tenhh')));
		
		$diadiems = $this->Diadiem->find('list',array('conditions'=>array('bac'=>0),
														'fields'=>array('id','tendd')));
		$this->set(compact('hanghoas','chitieungays','kenhs','diadiems'));
	}
	
	function hienkehoach($nhanvien_id = null,$donvi_id=null)
	{
		/*$tg = $this->Chitieunv->find('first',array('recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																		'Kehoach.ngaybd <='=>$now,
																		'Kehoach.ngaykt >='=>$now,
																		'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) <='=>7),
													'fields'=>array('Kehoach.id','Kehoach.ngaybd','Kehoach.ngaykt')));
		$kh = $tg['Kehoach']['id'];
		$ngaybd = $tg['Kehoach']['ngaybd'];
		$ngaykt  = $tg['Kehoach']['ngaykt'];*/
		if(!$donvi_id)
		{
			$donvi_id = $this->Session->read('cur_dv');
		}
		$this->Nhanvien->displayField='tennv';
		$nhanviens= $this->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id),));
		$now = date('Y-m-d');
		$ngaybd = $now;
		$ngaykt  = $now;
		$chitieungays = $this->Kehoachbhld->find('all',array(
											'recursive'=>1,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt),
											'order'=>array('thoigian DESC')));
		
		$hanghoas = $this->Hanghoabhld->find('list',array('fields'=>array('Hanghoabhld.hanghoa_id','Hanghoabhld.tenhh'),'order'=>array('Hanghoabhld.tenhh')));
		$this->set(compact('hanghoas','chitieungays','ngaybd','ngaykt','nhanvien_id','nhanviens','donvi_id'));
	}
	
	function laychitieu($nhanvien_id=null,$donvi_id)
	{
		$this->layout='ajax';
		
		$ngaybd = date('Y-m-d',strtotime($this->data['Chitieungay']['ngaybd']));
		$ngaykt  = date('Y-m-d',strtotime($this->data['Chitieungay']['ngaykt']));
		
		$nhanvien_id =$this->data['Chitieungay']['nhanvien'];
		if(empty($nhanvien_id))
		{
			$this->Nhanvien->displayField='id';
			 $nhanvien_id= $this->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id)));
		}
		/*$tg = $this->Chitieunv->find('first',array('recursive'=>0,
													'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																		'Kehoach.ngaybd <='=>$ngaybd,
																		'Kehoach.ngaykt >='=>$ngaykt,
																		'DATEDIFF(Kehoach.ngaykt,Kehoach.ngaybd) >'=>8),
													'fields'=>array('Kehoach.id','Kehoach.ngaybd','Kehoach.ngaykt')));
		$kh = $tg['Kehoach']['id'];*/
		
		$chitieungays = $this->Kehoachbhld->find('all',array(
											'recursive'=>1,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt),
											'order'=>array('thoigian DESC')));
		/*$hanghoas = $this->Chitieunv->find('list',array('recursive'=>0,
											'conditions'=>array('kehoach_id'=>$kh,
																'Chitieunv.nhanvien_id'=>$nhanvien_id),
											'fields'=>array('Hanghoa.id','Hanghoa.tenhh'),'order'=>array('Hanghoa.id DESC')));*/
		//debug($chitieungays);
		$hanghoas = $this->Hanghoabhld->find('list',array('fields'=>array('Hanghoabhld.hanghoa_id','Hanghoabhld.tenhh'),'order'=>array('Hanghoabhld.tenhh')));
		$nhanvienht = $this->Session->read('cur_manv');
		$this->set(compact('hanghoas','chitieungays','nhanvienht'));
	}
	
	function nhapkqbanhang($nhanvien_id=null,$ngay=null)
	{
		if(!$nhanvien_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
		if($nhanvien_id != $this->Session->read('cur_manv'))
		{
			$this->Session->setFlash(__('Không thể Sửa kết quả BHLD của nhân viên khác', true));
			$this->redirect(array('action'=>'hienkehoach',$nhanvien_id));
		}
			
		if(!empty($this->data))
		{	
				$record = array();
				$chitieus =array_keys($this->data['Chitieungay']['hanghoa']);
				foreach ($chitieus as $ct)
				{
					$record['Chitieungay']['id']=$ct;
					$record['Chitieungay']['ketqua']=($this->data['Chitieungay']['hanghoa'][$ct]==''?0:$this->data['Chitieungay']['hanghoa'][$ct]);
					$this->Chitieungay->saveAll($record);
				}
				$this->Session->setFlash(__('Đã lưu kết quả BHLD', true));
				$this->redirect(array('controller'=>'kqbanhangs','action'=>'kqbhld',$nhanvien_id));//$this->data['Chitieungay']['nhanvien_id']));
		}
		
		if(!$ngay)
		{
			$ngay = date('Y-m-d');
		}
		$chitieungays = $this->Kehoachbhld->find('all',array(
											'recursive'=>1,
											'conditions'=>array('nhanvien_id'=>$nhanvien_id,'thoigian'=>$ngay),
											'order'=>array('thoigian DESC')));
		$hanghoas = $this->Chitieungay->find('list',array('recursive'=>0,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,'thoigian'=>$ngay),
											'fields'=>array('Hanghoa.id','Hanghoa.tenhh'),
											'group'=>array('Hanghoa.id','Hanghoa.tenhh'),'order'=>array('Hanghoa.id DESC')));
		$ngay = date('d-m-Y',strtotime($ngay));
		$this->set(compact('chitieungays','hanghoas','nhanvien_id','ngay'));
		
	}

	function laycap2()
	{
		$this->layout = 'ajax';
		$dd_cha = $this->data['Chitieungay']['cap1'];
		$diadiems = $this->Diadiem->find('list',array('conditions'=>array('diadiem_id'=>$dd_cha),
														'fields'=>array('id','tendd')));
		$this->set(compact('diadiems'));
	}
	
	function laycap3()
	{
		$this->layout = 'ajax';
		if(isset($this->data['Chitieungay']['cap2']))
			$dd_cha = $this->data['Chitieungay']['cap2'];
		else
			$dd_cha = $this->data['cap2'];
		$diadiems = $this->Diadiem->find('list',array('conditions'=>array('diadiem_id'=>$dd_cha),
														'fields'=>array('id','tendd')));
		$this->set(compact('diadiems'));
	}
	
	function laycap4()
	{
		$this->layout = 'ajax';
		if(isset($this->data['Chitieungay']['cap3']))
			$dd_cha = $this->data['Chitieungay']['cap3'];
		else
			$dd_cha = $this->data['cap3'];
		$diadiems = $this->Diadiem->find('list',array('conditions'=>array('diadiem_id'=>$dd_cha),
														'fields'=>array('id','tendd')));
		$this->set(compact('diadiems'));
	}
	
	function emptycap4()
	{$this->layout = 'ajax';}
	
	function laykehoach($nhanvien_id)
	{
		$this->layout = 'ajax';
		$ngay =date('Y-m-d',strtotime($this->data['Chitieungay']['ngay']));
		$chitieungays = $this->Kehoachbhld->find('all',array(
											'recursive'=>1,
											'conditions'=>array('nhanvien_id'=>$nhanvien_id,'thoigian'=>$ngay),
											'order'=>array('thoigian DESC')));
		$hanghoas = $this->Chitieungay->find('list',array('recursive'=>0,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,'thoigian'=>$ngay),
											'fields'=>array('Hanghoa.id','Hanghoa.tenhh'),
											'group'=>array('Hanghoa.id','Hanghoa.tenhh'),'order'=>array('Hanghoa.id DESC')));
		$this->set(compact('chitieungays','hanghoas','nhanvien_id','ngay'));
	}

	function hienkehoachdv()
	{
			if(substr($this->Session->read('cur_tendv'),0,2)=='TT')
			{
				$donvi_id = $this->Session->read('cur_dv');
				$this->Donvi->displayField='tendv';
				$donvis = $this->Donvi->find('list',array('conditions'=>array('id'=>$donvi_id)));
			}
			else
			{
				$donvicha = $this->Session->read('cur_cha');
				$donvis = $this->Thuocdv->find('list',array('recursive'=>0,
											'conditions'=>array('donvi_id'=>$donvicha,
																'left(Donvi.madv,2)'=>'TT'),
											'fields'=>array('Donvi.id','Donvi.tendv')));
				$donvi_id = array_keys($donvis);
			}
		$this->Nhanvien->displayField='tennv';
		$nhanviens= $this->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id)));
		$this->set(compact('nhanviens','donvis'));
	}

	function laynv()
	{
		$this->layout = 'ajax';
		$donvi_id = $this->data['Chitieungay']['donvi_id'];
		if(empty($donvi_id))
		{
			if(substr($this->Session->read('cur_tendv'),0,2)=='TT')
			{
				$donvi_id = $this->Session->read('cur_dv');
				$this->Donvi->displayField='tendv';
				$donvis = $this->Donvi->find('list',array('conditions'=>array('id'=>$donvi_id)));
			}
			else
			{
				$donvicha = $this->Session->read('cur_cha');
				$donvis = $this->Thuocdv->find('list',array('recursive'=>0,
											'conditions'=>array('donvi_id'=>$donvicha,
																'left(Donvi.madv,2)'=>'TT'),
											'fields'=>array('Donvi.id','Donvi.tendv')));
				$donvi_id = array_keys($donvis);
			}
		}
		$nhanvien = $this->Phankenh->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id),
																	'fields'=>array('id','tennv')));
		$this->set('nhanvien',$nhanvien);
	}

	function laychitieudv()
	{
		$this->layout='ajax';
		
		$ngaybd = date('Y-m-d',strtotime($this->data['Chitieungay']['ngaybd']));
		$ngaykt  = date('Y-m-d',strtotime($this->data['Chitieungay']['ngaykt']));
		$nhanvien_id =$this->data['Chitieungay']['nhanvien'];
		$donvi_id = $this->data['Chitieungay']['donvi_id'];
		if(empty($donvi_id))
		{
			if(substr($this->Session->read('cur_tendv'),0,2)=='TT')
			{
				$donvi_id = $this->Session->read('cur_dv');
				$this->Donvi->displayField='tendv';
				$donvis = $this->Donvi->find('list',array('conditions'=>array('id'=>$donvi_id)));
			}
			else
			{
				$donvicha = $this->Session->read('cur_cha');
				$donvis = $this->Thuocdv->find('list',array('recursive'=>0,
											'conditions'=>array('donvi_id'=>$donvicha,
																'left(Donvi.madv,2)'=>'TT'),
											'fields'=>array('Donvi.id','Donvi.tendv')));
				$donvi_id = array_keys($donvis);
			}
		}
		if(empty($nhanvien_id))
		{
			$this->Nhanvien->displayField='id';
			 $nhanvien_id= $this->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id)));
		}
		
		$this->Kehoachbhld->unbindModel(array('hasMany'=>array('Nhansubhld')));
		$chitieungays = $this->Kehoachbhld->find('all',array(
											'recursive'=>2,
											'conditions'=>array('Kehoachbhld.nhanvien_id'=>$nhanvien_id,
																'thoigian >='=>$ngaybd,
																'thoigian <='=>$ngaykt),
											'order'=>array('thoigian DESC')));
		
		$hanghoas = $this->Hanghoabhld->find('list',array('fields'=>array('Hanghoabhld.hanghoa_id','Hanghoabhld.tenhh'),'order'=>array('Hanghoabhld.tenhh')));
		$nhanvienht = $this->Session->read('cur_manv');
		$this->set(compact('hanghoas','chitieungays','nhanvienht'));
	}
}